<?php

class Menu extends MY_Admin
{
	// Protected or private properties
	protected $_template;
	
	// Constructor
	public function __construct()
	{
		parent::__construct();

		// Check if the logged user is an administrator
		//$this->access_library->check_access();

		// Load needed models, libraries, helpers and language files
		$this->load->module_model('admin', 'menu_model', 'menu');
		$this->load->module_language('admin', 'general');
		$this->load->module_language('admin', 'menu');
		
	}
	
	public function index()
	{
		$this->access->restrict('Admin.Website.Menu.Index');
		$lang_working 		= load_language_working();
		$data['title'] 		= 'Quản lý Menu';
		$data['menus'] 		= $this->menu->get_all_menu();
		
		$this->_template['page']	= "website/menu/list";
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}
	
	public function add()
	{
		$this->access->restrict('Admin.Website.Menu.Add');
		$data['title'] 		= 'Thêm Menu';
		$data['treemenu']	=	$this->menu->get_tree_menu();
		$this->form_validation->set_rules('title', 'lang:form_title', 'required|max_length[60]|xss_clean');
		$this->form_validation->set_rules('description', 'lang:form_description', 'max_length[200]|xss_clean');
		$this->form_validation->set_error_delimiters('', '<br />');			
		if ($this->form_validation->run() == TRUE)
		{
			$this->menu->add_menu_item();
			$this->session->set_flashdata('message', lang('successfully_edited'));
			redirect('admin/website/menu', 'refresh');
		}

		$this->_template['page']	= 'website/menu/add';
			
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}
	
	public function edit($id)
	{
		$this->access->restrict('Admin.Website.Menu.Edit');
		$lang_working = load_language_working();
		$data['title'] 		= 'Sửa thông tin Menu';
		$data['treemenu']	=	$this->menu->get_tree_menu();
		$this->form_validation->set_rules('title', 'lang:form_title', 'required|max_length[60]|xss_clean');
		$this->form_validation->set_rules('description', 'lang:form_description', 'max_length[200]|xss_clean');

		$this->form_validation->set_error_delimiters('', '<br />');
			
		$data['menu'] = $this->menu->get_menu($id);
			
		if ($this->form_validation->run() == TRUE)
		{
			$this->menu->edit_menu($id);
			$this->session->set_flashdata('message', lang('successfully_edited'));
			redirect('admin/website/menu', 'refresh');
		}

		$this->_template['page']	= 'website/menu/edit';
			
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}
	
	
	
	public function enable($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
			
		$this->menu->change_status_menu($id,1);
		redirect('admin/website/menu', 'refresh');
		
	}
	
	public function disable($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
			
		$this->menu->change_status_menu($id,0);
		redirect('admin/website/menu', 'refresh');
	}
	
	public function delete($id)
	{
		$this->menu->delete_menu($id);
		$this->session->set_flashdata('message', lang('successfully_deleted'));

		redirect('admin/website/menu', 'refresh');
	}
	
	public function move_menu_item()
	{
		$item = $this->input->post('item');
		$item_id = explode('_', $item);
		$action = $this->input->post('action');
		
		if ($action == 'up')
		{
			$this->menu->move_item_up($item_id[1]);
		}
		else if ($action == 'down')
		{
			$this->menu->move_item_down($item_id[1]);
		}
	}
	
}

/* End of file links.php */
/* Location: ./application/modules/admin/controllers/links.php */